package com.mera.tektronix.csviewer.domain



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Department)
class DepartmentTests {

	Company company
	Department department
	
	@Before
	void setUp() {
		mockForConstraintsTests(Department)
		company = new Company(name: "Mera", email: "mera@mera.ru", address:"Nizhny Novgorod, Delovaya 13", phone: "278-88-88")
		department = new Department(name: "Department", email: "department@mera.ru", phone: "278-88-88#0001", company: company)
	}
	
	void testConstraintsNameUnique() {
		Department test = new Department(name: "Department", email: "department@mera.ru", phone: "278-88-88#0001")
		mockForConstraintsTests(Department, [test])
  
		assertFalse department.validate()
		assertEquals 'unique', department.errors['name']
  
		department.name = "Another department"
		assertTrue department.validate()
		assertNull department.errors['name']
	}
	
	void testConstraintsNameBlank() {
		department.name = ""
		assertFalse department.validate()
		assertEquals 'blank', department.errors['name']

		department.name = "Department"
		assertTrue department.validate()
		assertNull department.errors['name']
	}
	
	void testConstraintsEmailEmail() {
		department.email = 'badEmail'
		assertFalse department.validate()
		assertEquals 'email', department.errors['email']

		department.email = 'mera@mera.ru'
		assertTrue department.validate()
		assertNull department.errors['email']
	}
	
	void testConstraintsPhoneMatches() {
		department.phone = "235678"
		assertFalse department.validate()
		assertEquals 'matches', department.errors['phone']
		
		department.phone = "278-88-65"
		assertTrue department.validate()
		assertNull department.errors['phone']
	}
	
	void testConstraintsCompanyNullable() {
		department.company = null
		assertFalse department.validate()
		assertEquals 'nullable', department.errors['company']

		department.company = company
		assertTrue department.validate()
		assertNull department.errors['company']
	}
}
